const flow = require('nimble');
const { exec } = require('child_process');

function downloadNodeVersion(version, destination, callback) {
  const url = `http://nodejs.org/dist/node-v${version}.tar.gz`;
  const filepath = `${destination}/${version}.tgz`;
  exec(`curl ${url} >${filepath}`, callback);
}

flow.series([
  function (callback) {
    flow.parallel(
      [
        function (callback) {
          console.log('Downloading Node v0.4.6...');
          downloadNodeVersion('0.4.6', './chapter03/flow/temp', callback);
        },
        function (callback) {
          console.log('Downloading Node v0.4.7...');
          downloadNodeVersion('0.4.7', './chapter03/flow/temp', callback);
        }
      ],
      callback
    );
  },
  function (callback) {
    console.log('Creating archive of downloaded files...');
    exec(
      'tar cvf ./chapter03/flow/node_distros.tar ./chapter03/flow/temp/0.4.6.tgz ./chapter03/flow/temp/0.4.7.tgz',
      function (err, stdout, stderr) {
        console.log('All done!');
        callback();
      }
    );
  }
]);
